/**
 *
 * @returns
 */
export function useCookie() {
  const assign = (target) => {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        target[key] = source[key];
      }
    }
    return target;
  };
  const setCookie = (key, value, attributes) => {
    if (typeof document === "undefined") {
      return;
    }

    attributes = assign({}, { path: "/" }, attributes);

    if (typeof attributes.expires === "number") {
      attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
    }
    if (attributes.expires) {
      attributes.expires = attributes.expires.toUTCString();
    }

    key = encodeURIComponent(key)
      .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
      .replace(/[()]/g, escape);

    var stringifiedAttributes = "";
    for (var attributeName in attributes) {
      if (!attributes[attributeName]) {
        continue;
      }

      stringifiedAttributes += "; " + attributeName;

      if (attributes[attributeName] === true) {
        continue;
      }
      stringifiedAttributes += "=" + attributes[attributeName].split(";")[0];
    }

    return (document.cookie =
      key + "=" + converter.write(value, key) + stringifiedAttributes);
  };
  const converter = {
    read: function (value) {
      if (value[0] === '"') {
        value = value.slice(1, -1);
      }
      return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
    },
    write: function (value) {
      return encodeURIComponent(value).replace(
        /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
        decodeURIComponent
      );
    },
  };
  const getCookie = (key) => {
    const cookies = document.cookie ? document.cookie.split("; ") : [];
    const jar = {};
    for (let i = 0; i < cookies.length; i++) {
      const parts = cookies[i].split("=");
      const value = parts.slice(1).join("=");

      try {
        const foundKey = decodeURIComponent(parts[0]);
        jar[foundKey] = converter.read(value, foundKey);

        if (key === foundKey) {
          break;
        }
      } catch (e) {}
    }
    return key ? jar[key] : jar;
  };

  const clear = () => document.cookie = "";
  return {
    setCookie,
    getCookie,
  };
}
